//
//  SubjectCell.m
//  LimitFree
//
//  Created by LZXuan on 15-6-9.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "SubjectCell.h"
#import "SubjectAppView.h"
#import "UIImageView+WebCache.h"

@interface SubjectCell ()
@property (nonatomic,copy) SubjectBlock myBlock;
@end

@implementation SubjectCell

- (void)awakeFromNib {
    
    //把 cell 的选中效果 屏蔽
    self.selectionStyle = UITableViewCellSelectionStyleNone;//无选中效果
    
    
    //初始化的时候 创建 四个 右侧appView粘贴到cell得contentView上
    //要去加载xib 中 View
    for (NSInteger i = 0; i < 4; i++) {
        SubjectAppView *appView = [[[NSBundle mainBundle] loadNibNamed:@"SubjectAppView" owner:nil options:nil] lastObject];
        CGFloat h = self.imgImageView.frame.size.height/4;
        CGFloat w = kScreenSize.width-self.imgImageView.frame.origin.x - self.imgImageView.frame.size.width;
        
        CGFloat x =self.imgImageView.frame.origin.x + self.imgImageView.frame.size.width;
        appView.frame = CGRectMake(x, self.imgImageView.frame.origin.y+h*i, w, h);
        appView.tag = 101+i;
        //增加事件
        [appView addTarget:self action:@selector(appViewClick:)];
        //粘贴到contentView
        [self.contentView addSubview:appView];
    }
}
- (void)appViewClick:(SubjectAppView *)appView {
    //要委托 cell 所在的视图控制器界面进行界面跳转
    
    if (self.myBlock) {
        //把model传入
        self.myBlock(appView.model);
    }
}
//填充cell
- (void)showDataWithModel:(SubjectModel *)model withBlock:(SubjectBlock)myBlock {
    //保存
    self.myBlock = myBlock;
    
    self.nameLabel.text = model.title;
    [self.imgImageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed: @"topic_Header"]];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.desc_img] placeholderImage:[UIImage imageNamed: @"topic_Header"]];
    self.textView.text = model.desc;
    //四个appView
    for (NSInteger i = 0; i < model.applications.count; i++) {
        //获取右侧appView
        SubjectAppView *appView =(SubjectAppView*) [self.contentView viewWithTag:101+i];
        //填充
        [appView showDataWithAppModel:model.applications[i]];
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
